#include "itsaction.h"

namespace IAlib
{

ItsAction::ItsAction():m_action_id(""), m_actor_id(""), m_exe_time(0), m_taken_time(0), m_result(true)
{
}

ItsAction::ItsAction(const ItsAction &action)
{
    m_action_id = action.m_action_id;
    m_action_id = action.m_actor_id;

    m_exe_time = action.m_exe_time;
    m_taken_time = action.m_taken_time;

	m_result = action.m_result;

    m_necessary_artefacts = action.m_necessary_artefacts;
    m_parameters = action.m_parameters;
}

void ItsAction::addNecessaryArtefact(const QString &artefact)
{
    m_necessary_artefacts.append(artefact);
}

void ItsAction::addParameters(const QString &param)
{
    m_parameters.append(param);
}

void ItsAction::removeNecessaryArtefact(const QString &artefact)
{
    if (m_necessary_artefacts.contains(artefact))
    {
        m_necessary_artefacts.removeAt(m_necessary_artefacts.indexOf(artefact));
    }
}

void ItsAction::removeParameters(const QString &param)
{
    if (m_parameters.contains(param))
    {
        m_parameters.removeAt(m_parameters.indexOf(param));
    }
}

QDomElement ItsAction::toXML(QDomDocument &doc)
{
    QDomElement ele = doc.createElement("ItsAction");
    ele.setAttribute("id",m_action_id);
    ele.setAttribute("actor_id",m_action_id);

    ele.setAttribute("exeTime",m_exe_time);
    ele.setAttribute("takenTime",m_taken_time);


    QDomElement necessary_artefacts = doc.createElement("necessaryArtefacts");
    for (int i = 0; i < m_necessary_artefacts.size(); i++)
    {
        QDomElement artefact = doc.createElement("artefact");
        artefact.appendChild(doc.createTextNode(m_necessary_artefacts[i]));
        necessary_artefacts.appendChild(artefact);
    }
    ele.appendChild(necessary_artefacts);
    QDomElement parameters = doc.createElement("parameters");
    for (int i = 0; i < m_parameters.size(); i++)
    {
        QDomElement parameter = doc.createElement("parameter");
        parameter.appendChild(doc.createTextNode(m_parameters[i]));
        parameters.appendChild(parameter);
    }
    ele.appendChild(parameters);
    return ele;
}

void ItsAction::fromXML(QDomElement ele)
{
    m_action_id = ele.attribute("id","");
    m_actor_id = ele.attribute("actor_id","");

    m_exe_time = ele.attribute("exeTime","").toInt();
    m_taken_time = ele.attribute("takenTime","").toInt();

    QDomNode n = ele.firstChild();
    while( !n.isNull() )
    {
        QDomElement e = n.toElement();
        if( !e.isNull() )
        {
            if( e.tagName() == "necessaryArtefacts" )
            {
                QDomNode artefact = e.firstChild();
                while (!artefact.isNull())
                {
                    QDomElement ele = artefact.toElement();
                    addNecessaryArtefact(ele.text());
                    artefact = artefact.nextSibling();
                }
            }
            else if (e.tagName() == "parameters")
            {
                QDomNode parameter = e.firstChild();
                while (!parameter.isNull())
                {
                    QDomElement ele = parameter.toElement();
                    addParameters(ele.text());
                    parameter = parameter.nextSibling();
                }
            }
        }
        n = n.nextSibling();
    }
}

QByteArray ItsAction::toByteArray() const
{
    QByteArray byte_array;
    QDataStream data(&byte_array, QIODevice::WriteOnly);
    data<<m_action_id;
    data<<m_actor_id;
    data<<m_exe_time;
    data<<m_taken_time;
    data<<m_necessary_artefacts.size();
    for (int i = 0; i < m_necessary_artefacts.size(); i++)
    {
        data<<m_necessary_artefacts[i];
    }
    data<<m_parameters.size();
    for (int i = 0; i < m_parameters.size(); i++)
    {
        data<<m_parameters[i];
    }
    return byte_array;
}

//-----------------------------------------------------

void ItsAction::fromByteArray(QByteArray& byte_array)
{
    QDataStream data(&byte_array, QIODevice::ReadOnly);
    data>>m_action_id;
    data>>m_actor_id;

    data>>m_exe_time;
    data>>m_taken_time;
    int nb_artefact = 0;
    data>>nb_artefact;
    int i =  0;
    while ( i < nb_artefact )
    {
        QString artefact;
        data>>artefact;
        addNecessaryArtefact(artefact);
        i++;
    }
    int nb_parameters = 0;
    data>>nb_parameters;
    i = 0;
    while ( i < nb_parameters)
    {
        QString parametre;
        data>>parametre;
        addParameters(parametre);
        i++;
    }
}

bool ItsAction::equal(const ItsAction &action) const
{
    /*if (m_action_id == action.m_action_id && m_id_target == action.m_id_target && m_id_trigger == action.m_id_trigger)
    {
        return true;
    }
    else
    {
        return false;
    }*/
    return true;
}

ItsAction& ItsAction::operator=(const ItsAction& action)
{
    m_action_id = action.m_action_id;
    m_actor_id = action.m_action_id;

    m_exe_time = action.m_exe_time;
    m_taken_time = action.m_taken_time;

	m_result = action.m_result;

    m_necessary_artefacts = action.m_necessary_artefacts;
    m_parameters = action.m_parameters;

    return *this;
}

bool ItsAction::operator==(const ItsAction& action) const
{
    /*if (m_action_id == action.m_action_id && m_id_target == action.m_id_target && m_id_trigger == action.m_id_trigger)
    {
        return true;
    }
    else
    {
        return false;
    }*/
    return true;
}

}


